package com.yuehuanghun.admin.core.controller.monitor;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysLogininfor;
import com.yuehuanghun.admin.core.service.ISysLogininforService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.web.BaseResource;
import com.yuehuanghun.mybatis.milu.data.Sort;
import com.yuehuanghun.mybatis.milu.data.Sort.Direction;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 系统访问记录
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/monitor/logininfor")
@SaCheckLogin
public class SysLogininforController extends BaseResource {
	@Autowired
	private ISysLogininforService logininforService;

	@SuppressWarnings("unchecked")
	@SaCheckPermission("monitor:logininfor:list")
	@GetMapping("/list")
	public PageDTO<SysLogininfor> list(SysLogininfor logininfor) {
		List<SysLogininfor> list = logininforService.getByExampleAndSort(logininfor, Sort.by(Direction.DESC, SysLogininfor::getInfoId), getPage());
		return toPage(list);
	}

	@Log(title = "登录日志", businessType = BusinessType.EXPORT)
	@SaCheckPermission("monitor:logininfor:export")
	@GetMapping("/export")
	public String export(SysLogininfor logininfor) {
		List<SysLogininfor> list = logininforService.getByExample(logininfor);
		ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
		return util.exportExcel(list, "登录日志");
	}

	@Log(title = "登录日志", businessType = BusinessType.DELETE)
	@SaCheckPermission("monitor:logininfor:remove")
	@DeleteMapping("/{infoIds}")
	public int remove(@PathVariable List<Long> infoIds) {
		return logininforService.deleteByIds(infoIds);
	}

	@Log(title = "登录日志", businessType = BusinessType.CLEAN)
	@SaCheckPermission("monitor:logininfor:remove")
	@DeleteMapping("/clean")
	public void clean() {
		logininforService.deleteByCriteria(p -> {}); //清空所有，不要使用truncate table
	}
}
